﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using WebApi.Areas.Service.Models;

namespace WebApi.Areas.Service.Controllers
{
    public class LocationController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Me([FromUri]InputLocationModel model)
        {
            var data = new ReturnData<object>();
            if (ModelState.IsValid && model != null)
            {
                var client = new MongoClient("mongodb://atsoftware.vn:27017");
                var server = client.GetServer();
                var database = server.GetDatabase("sfood");
                var food = database.GetCollection<Food>("server_food");
                var query = Query<Food>.Where(x=>x.location.latitude == model.latitude && x.location.longitude == model.longitude);
                data.data = food.Find(query).Select(x => new { x.city, x.district }).ToList();
                data.success = true;
                return Request.CreateResponse(HttpStatusCode.OK, data);
            }
            data.success = false;
            data.error = "Tham số không đúng định dạng";
            return Request.CreateResponse(HttpStatusCode.OK, data);
        }
    }
}
